package cn.fws.meap.utils;

import cn.fws.framework.utils.UUIDUtils;

import java.io.UnsupportedEncodingException;


/**
 * 微信支付API工具类
 * 返回字符串格式的xml
 */
public class WeChatPayUtils {
    /**
     * 统一下单
     *
     * @param appId
     * @param mch_id
     * @param signKey
     * @return
     */
    public static String unifiedOrder(String appId, String mch_id, String signKey) {
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("<xml>");
        strBuilder.append("<appid>" + appId + "</appid>");
        strBuilder.append("<attach>支付测试</attach>");
        strBuilder.append("<body>JSAPI支付测试</body>");
        strBuilder.append("<mch_id>" + mch_id + "</mch_id>");
        strBuilder.append("<detail><![CDATA[{ \"goods_detail\":[ { \"goods_id\":\"iphone6s_16G\", \"wxpay_goods_id\":\"1001\", \"goods_name\":\"iPhone6s 16G\", \"quantity\":1, \"price\":528800, \"goods_category\":\"123456\", \"body\":\"苹果手机\" }, { \"goods_id\":\"iphone6s_32G\", \"wxpay_goods_id\":\"1002\", \"goods_name\":\"iPhone6s 32G\", \"quantity\":1, \"price\":608800, \"goods_category\":\"123789\", \"body\":\"苹果手机\" } ] }]]></detail>");
        strBuilder.append("<nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>");
        strBuilder.append("<notify_url>http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>");
        strBuilder.append("<openid>o5ct_4jpXZAW_QRB8U1bKcFPnqM8</openid>");
        strBuilder.append("<out_trade_no>1415659990</out_trade_no>");
        strBuilder.append("<spbill_create_ip>14.23.150.211</spbill_create_ip>");
        strBuilder.append("<total_fee>1</total_fee>");
        strBuilder.append("<trade_type>JSAPI</trade_type>");
        strBuilder.append("<sign>" + signKey + "</sign>");
        strBuilder.append("</xml>");

        String text = strBuilder.toString();
        try {
            String unifiedOrderXml = new String(text.getBytes(), "utf-8");
            return unifiedOrderXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 查询订单
     *
     * @param appId
     * @param mch_id
     * @param signKey
     * @return
     */
    public static String orderQuery(String appId, String mch_id, String signKey) {
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("<xml>");
        strBuilder.append("<appid>" + appId + "</appid>");
        strBuilder.append("<mch_id>" + mch_id + "</mch_id>");
        strBuilder.append("<nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>");
        strBuilder.append("<transaction_id>1008450740201411110005820873</transaction_id>");
        strBuilder.append("<sign>" + signKey + "</sign>");
        strBuilder.append("</xml>");

        String text = strBuilder.toString();
        try {
            String orderQueryXml = new String(text.getBytes(), "utf-8");
            return orderQueryXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 关闭订单
     *
     * @param appId
     * @param mch_id
     * @param signKey
     * @return
     */
    public static String closeOrder(String appId, String mch_id, String signKey) {
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("<xml>");
        strBuilder.append("<appid>" + appId + "</appid>");
        strBuilder.append("<mch_id>" + mch_id + "</mch_id>");
        strBuilder.append("<nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>");
        strBuilder.append("<out_trade_no>1415983244</out_trade_no>");
        strBuilder.append("<sign>" + signKey + "</sign>");
        strBuilder.append("</xml>");

        String text = strBuilder.toString();
        try {
            String closeOrderXml = new String(text.getBytes(), "utf-8");
            return closeOrderXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 申请退款
     *
     * @param appId
     * @param mch_id
     * @param signKey
     * @return
     */
    public static String refund(String appId, String mch_id, String signKey,int refund_fee,int total_fee,String orderNumber) {
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("<xml>");
        strBuilder.append("<appid>" + appId + "</appid>");
        strBuilder.append("<mch_id>" + mch_id + "</mch_id>");
        strBuilder.append("<nonce_str>"+ UUIDUtils.getUUID() +"</nonce_str>");
        strBuilder.append("<out_refund_no>"+ UUIDUtils.getUUID()+UUIDUtils.getUUID() +"</out_refund_no>");
        strBuilder.append("<out_trade_no>"+orderNumber+"</out_trade_no>");
        strBuilder.append("<refund_fee>"+refund_fee+"</refund_fee>");
        strBuilder.append("<total_fee>"+total_fee+"</total_fee>");
        strBuilder.append("<transaction_id></transaction_id>");
        strBuilder.append("<sign>" + signKey + "</sign>");
        strBuilder.append("</xml>");

        String text = strBuilder.toString();
        try {
            String refundXml = new String(text.getBytes(), "utf-8");
            return refundXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 查询退款
     *
     * @param appId
     * @param mch_id
     * @param signKey
     * @return
     */
    public static String refundQuery(String appId, String mch_id, String signKey) {
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("<xml>");
        strBuilder.append("<appid>" + appId + "</appid>");
        strBuilder.append("<mch_id>" + mch_id + "</mch_id>");
        strBuilder.append("<nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>");
        strBuilder.append("<out_refund_no></out_refund_no>");
        strBuilder.append("<out_trade_no>1415757673</out_trade_no>");
        strBuilder.append("<refund_id></refund_id>");
        strBuilder.append("<transaction_id></transaction_id>");
        strBuilder.append("<sign>" + signKey + "</sign>");
        strBuilder.append("</xml>");

        String text = strBuilder.toString();
        try {
            String refundQueryXml = new String(text.getBytes(), "utf-8");
            return refundQueryXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 下载对账单
     *
     * @param appId
     * @param mch_id
     * @param signKey
     * @return
     */
    public static String downloadBill(String appId, String mch_id, String signKey) {
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("<xml>");
        strBuilder.append("<appid>" + appId + "</appid>");
        strBuilder.append("<bill_date>20141110</bill_date>");
        strBuilder.append("<bill_type>ALL</bill_type>");
        strBuilder.append("<mch_id>" + mch_id + "</mch_id>");
        strBuilder.append("<nonce_str>21df7dc9cd8616b56919f20d9f679233</nonce_str>");
        strBuilder.append("<sign>" + signKey + "</sign>");
        strBuilder.append("</xml>");

        String text = strBuilder.toString();
        try {
            String downloadBillXml = new String(text.getBytes(), "utf-8");
            return downloadBillXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 下载资金账单
     *
     * @param appId
     * @param mch_id
     * @param signKey
     * @return
     */
    public static String downloadFundFlow(String appId, String mch_id, String signKey) {
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("<xml>");
        strBuilder.append("<appid>" + appId + "</appid>");
        strBuilder.append("<bill_date>20141110</bill_date>");
        strBuilder.append("<account_type>Basic</account_type>");
        strBuilder.append("<mch_id>" + mch_id + "</mch_id>");
        strBuilder.append("<nonce_str>21df7dc9cd8616b56919f20d9f679233</nonce_str>");
        strBuilder.append("<sign>" + signKey + "</sign>");
        strBuilder.append("</xml>");

        String text = strBuilder.toString();
        try {
            String downloadFundFlowXml = new String(text.getBytes(), "utf-8");
            return downloadFundFlowXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 支付结果通知
     *
     * @param appId
     * @param mch_id
     * @param signKey
     * @return
     */
    public static String payAction(String appId, String mch_id, String signKey) {
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("<xml>");
        strBuilder.append("<appid><![CDATA[" + appId + "]]></appid>");
        strBuilder.append("<attach><![CDATA[支付测试]]></attach>");
        strBuilder.append("<bank_type><![CDATA[CFT]]></bank_type>");
        strBuilder.append("<fee_type><![CDATA[CNY]]></fee_type>");
        strBuilder.append("<is_subscribe><![CDATA[Y]]></is_subscribe>");
        strBuilder.append("<mch_id><![CDATA[" + mch_id + "]]></mch_id>");
        strBuilder.append("<nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str>");
        strBuilder.append("<openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid>");
        strBuilder.append("<out_trade_no><![CDATA[1409811653]]></out_trade_no>");
        strBuilder.append("<result_code><![CDATA[SUCCESS]]></result_code>");
        strBuilder.append("<return_code><![CDATA[SUCCESS]]></return_code>");
        strBuilder.append("<sign><![CDATA[" + signKey + "]]></sign>");
        strBuilder.append("<time_end><![CDATA[20140903131540]]></time_end>");
        strBuilder.append("<total_fee>1</total_fee>");
        strBuilder.append("<coupon_fee><![CDATA[10]]></coupon_fee>");
        strBuilder.append("<coupon_count><![CDATA[1]]></coupon_count>");
        strBuilder.append("<coupon_type><![CDATA[CASH]]></coupon_type>");
        strBuilder.append("<coupon_id><![CDATA[10000]]></coupon_id>");
        strBuilder.append("<coupon_fee><![CDATA[100]]></coupon_fee>");
        strBuilder.append("<trade_type><![CDATA[JSAPI]]></trade_type>");
        strBuilder.append("<transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>");
        strBuilder.append("</xml>");

        String text = strBuilder.toString();
        try {
            String payActionXml = new String(text.getBytes(), "utf-8");
            return payActionXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 退款结果通知
     *
     * @param appId
     * @param mch_id
     * @param signKey
     * @return
     */
    public static String backMoney(String appId, String mch_id) {
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("<xml>");
        strBuilder.append("<return_code>SUCCESS</return_code>");
        strBuilder.append("<appid><![CDATA[" + appId + "]]></appid>");
        strBuilder.append("<mch_id><![CDATA[" + mch_id + "]]></mch_id>");
        strBuilder.append("<nonce_str><![CDATA[TeqClE3i0mvn3DrK]]></nonce_str>");
        strBuilder.append("<req_info><![CDATA[T87GAHG17TGAHG1TGHAHAHA1Y1CIOA9UGJH1GAHV871HAGAGQYQQPOOJMXNBCXBVNMNMAJAA]]></req_info>");
        strBuilder.append("</xml>");

        String text = strBuilder.toString();
        try {
            String backMoneyXml = new String(text.getBytes(), "utf-8");
            return backMoneyXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 拉取订单评价数据
     *
     * @param appId
     * @param mch_id
     * @param signKey
     * @return
     */
    public static String batchQueryComment(String appId, String mch_id, String signKey) {
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("<xml>");
        strBuilder.append("<appid>" + appId + "</appid>");
        strBuilder.append("<mch_id>" + mch_id + "</mch_id>");
        strBuilder.append("<nonce_str>5K8264ILTKCH16CQ2502SI8ZNMTM67VS</nonce_str>");
        strBuilder.append("<offset>0</offset>");
        strBuilder.append("<limit>100</limit>");
        strBuilder.append("<begin_time>20170724000000</begin_time>");
        strBuilder.append("<end_time>20170725000000</end_time>");
        strBuilder.append("<sign_type>HMAC-SHA256</sign_type>");
        strBuilder.append("<sign>" + signKey + "</sign>");
        strBuilder.append("</xml>");

        String text = strBuilder.toString();
        try {
            String batchQueryCommentXml = new String(text.getBytes(), "utf-8");
            return batchQueryCommentXml;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


    }


}
